﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Theme_Index_rpt_sturank : System.Web.UI.UserControl
{
    PowerCreator.LMS.ViewQuery.ViewQuery objViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        BinderstuRank();
    }
    //绑定学生名次：
    private void BinderstuRank()
    {
        string searchView = "";
        int intScoresCol = 0;
        int intScores = 0;
        int intHours = 0;
        int intWorkNum = 0;
        int intWorReply = 0;
        int intExam = 0;
        string searchStu = "select top 6 * from to_learner ";
        DataTable dtStu = objViewQuery.Query(searchStu);
        dtStu.Columns.Add("stuScore", typeof(int));
        dtStu.Columns.Add("levelStu");
        if (dtStu.Rows.Count > 0)
        {
            for (int j = 0; j < dtStu.Rows.Count; j++)
            {
                //得到每个学员的总学时 
                //string searchCourse = "select sum(SscrlogTotalTime) as studyHours,FKSscrlogUserID from TStudentStudyCouReslog group by FKSscrlogUserID having FKSscrlogUserID={0}";
                //searchView = string.Format(searchCourse, dtStu.Rows[j]["Learner_Id"].ToString());
                //DataTable dtCourse = objViewQuery.Query(searchView);
                //if (dtCourse.Rows.Count > 0)
                //{
                //    if (string.IsNullOrEmpty(dtCourse.Rows[0]["studyHours"].ToString()))
                //    {
                //        intHours = 0;
                //    }
                //    else
                //    {
                //        intHours = int.Parse(dtCourse.Rows[0]["studyHours"].ToString());
                //    }

                //}
                //得到作业完成总次数
                string searchWorkNum = "select count(Creator) as WorkNum from TO_ClassTasks where Creator ={0}";
                searchView = string.Format(searchWorkNum, dtStu.Rows[j]["Learner_Id"].ToString());
                DataTable dtWorkNum = objViewQuery.Query(searchView);
                if (dtWorkNum.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(dtWorkNum.Rows[0]["WorkNum"].ToString()))
                    {
                        intWorkNum = 0;
                    }
                    else
                    {
                        intWorkNum = int.Parse(dtWorkNum.Rows[0]["WorkNum"].ToString());
                    }

                }
                else {
                    intWorkNum = 0;
                }
                //得到作业评论总次数
                string searchTasksReply = "select count(*) as ReplyNum ,Creator from (select  b.Creator from TO_ClassTasksReply as a ,TO_UserClassTask as b ,TO_ClassTasks as c  where a.UserTask_Id = b.UserTask_Id and b.task_Id = c.ClassTasks_Id ) as tb group by Creator  having Creator ={0} ";
                searchView = string.Format(searchTasksReply, dtStu.Rows[j]["Learner_Id"].ToString());
                DataTable dtTasksReply = objViewQuery.Query(searchView);
                if (dtTasksReply.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(dtTasksReply.Rows[0]["ReplyNum"].ToString()))
                    {
                        intWorReply = 0;
                    }
                    else
                    {
                        intWorReply = int.Parse(dtTasksReply.Rows[0]["ReplyNum"].ToString());
                    }

                }
                else {
                    intWorReply = 0;
                }
                //得到考试总成绩
                string searchExam = "select sum(UPHistoryScore) as UPHistoryScore from  UserPaperHistory where fKUPHistoryUserID ={0}";
                searchView = string.Format(searchExam, dtStu.Rows[j]["Learner_Id"].ToString());
                DataTable dtExam = objViewQuery.Query(searchView);
                if (dtExam.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(dtExam.Rows[0]["UPHistoryScore"].ToString()))
                    {
                        intExam = 0;
                    }
                    else
                    {
                        intExam = int.Parse(dtExam.Rows[0]["UPHistoryScore"].ToString());
                    }

                }
                else {
                    intExam = 0;
                }
                intScores = 0;
                intScores = intWorkNum + intWorReply + intExam;//得到每个学员的分数
                dtStu.Rows[j]["stuScore"] = intScores;
                //dtStu.Rows[j]["stuScore"] = 0;
            }
        }
        //学生
        DataView dv = dtStu.DefaultView;
        if (dtStu.Rows.Count > 0)
        {
            dv.Sort = "stuScore desc";
            dtStu = dv.ToTable();
            for (int j = 0; j < dtStu.Rows.Count; j++)
            {
                dtStu.Rows[j]["levelStu"] = j + 1;
            }
        }

        if (dtStu.Rows.Count > 6)
        {
            for (int k = 6; k < dtStu.Rows.Count; k++)
            {
                dtStu.Rows.RemoveAt(k);
                k--;
            }
        }
        rpt_StuRank.DataSource = dtStu;
        rpt_StuRank.DataBind();
    }
}